<?php
import("Applications.Bibliotheque.Classes.DAO.RoleDAO");

$application = $response->getAttribute("application");

$oeuvre = $response->getAttribute("object");
$objectType = $response->getAttribute("objectType");
$typeId = $response->getAttribute("typeId");

$roleDao = DAOFactory::getDAO("Role");
$roles = $roleDao->retrieveAll();

$types = null ;
if ($typeId == null) {
    $typeDao = DAOFactory::getDAO("Type");
    $types = $typeDao->retrieveBaseTypes();
}

$contributions = null ;
if ($oeuvre != null) {
    $contributions = $oeuvre->getContributions();
    if (count($contributions) == 0) {
        $contributions = null ;
    }
}
?>
<script type="text/javascript">

    var listOfRoles = new Array();
    
    <?php
    if (count($roles) > 0) {
        $i = 0 ;
        foreach ($roles as $role) {
            echo "listOfRoles[".$i."] = getRole('".$role->getId()."', '".$role->getNom()."');\n" ;
            $i++ ;
        }
    }
    ?>

    var contributions = new Array();
    
    <?php    
    if ($contributions != null) {
        $i =0 ;
        foreach ($contributions as $contribution) {
            if (
                $contribution != null &&
                $contribution->getAuteur() != null &&
                $contribution->getRole() != null
            ) {
                echo "contributions[".$i."] = ".
                        "getContribution(getAuteur(".
                            "'".$contribution->getAuteur()->getId()."', ".
                            "'".$contribution->getAuteur()->toString()."'".
                        "), ".
                        "getRole(".
                            "'".$contribution->getRole()->getId()."', ".
                            "'".$contribution->getRole()->getNom()."'".
                        ")".
                    ");\n" ;
                $i++ ;
            }
        }
    } else {
        echo "contributions[0] = getEmptyContribution();\n" ;
    }
    ?>

    var contributionIndex = contributions.length ;
</script>
<form
    class="editForm"
    method="POST"
    action="<?php Printer::display(HTML::linkHref('action=CMS.Save&type'.$objectType));?>">
    <input
        type="hidden"
        name="id"
        value="<?php if ($oeuvre != null) Printer::display($oeuvre->getId()); ?>" />
    <span class="label">
        <span class="titre">Titre :</span>
        <input
            class="firstInput"
            type="text"
            name="titre"
            value="<?php if ($oeuvre != null) Printer::display($oeuvre->getTitre()); ?>" />
    </span>
    <span class="label">
        <span class="titre">Annee :</span>
        <input
            class="firstInput"
            type="text"
            name="annee"
            value="<?php if ($oeuvre != null) Printer::display($oeuvre->getAnnee()); ?>" />
    </span>
    <span class="label" id="auteursSpan">
        <span class="titre">Auteurs :</span>
        <input
            type="button"
            name="addAuteur"
            value="+"
            onclick="javascript:addNewContribution(
                    this.parentNode,
                    listOfRoles,
                    contributionIndex
                ); contributionIndex++;"
            class="firstInput" />
        <script type="text/javascript">
            for(var i=0; i<contributionIndex; i++) {
                addContribution(document.getElementById('auteursSpan'), contributions[i], listOfRoles, i);
            }
        </script>
    </span>
    <span class="label">
        <span class="titre">Notes :</span>
        <input
            class="firstInput"
            type="text"
            name="notes"
            value="<?php if ($oeuvre != null) Printer::display($oeuvre->getNotes()); ?>" />
    </span>
    <span class="label">
        <span class="titre">Pret :</span>
        <input
            class="firstInput"
            type="text"
            name="pret"
            value="<?php if ($oeuvre != null) Printer::display($oeuvre->getPret()); ?>" />
    </span>
    <?php 
    $selectedId = null ;
    if ($oeuvre != null && $oeuvre->getType() != null) {
        $selectedId = $oeuvre->getType()->getId();
    }
    $response->setAttribute("selectedId", $selectedId);
    $response->setAttribute("types", $types);
    $response->setAttribute("inputName", "type_id");
    $response->setAttribute("title", "Type :");
    TemplateManager::display(
        $application,
        "main/cms/edit/elements/typeSelectList",
        $response
    )
    ?>
    <input type="submit" value="OK" />
</form>
